<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<meta http-equiv="pragma" content="no-cache" />
<base href="<%=basePath%>">
<script type="text/javascript" src="static/js/jquery-3.2.1.js"></script>
<style type="text/css">
body{ text-align:center} 
.div{ margin:0 auto; width:400px; height:100px; border:1px solid #F00} 
/* css注释：为了观察效果设置宽度 边框 高度等样式 */ 
</style>
</head>
<body>
<div>
<img id="qrcode" alt="" src="static/images/qrcode.jpg"></br>
<a id="login" href=""></a></br></br>
<div id="qrstate"></div></br>
<input id="refurbish" type = "button" value="强制登陆" onclick="login()">
</div>
${qrstate }
</body>
<script type="text/javascript">
	var timer;
	/*获取二维码验证状态*/
	function checkQrcode(){
		$.ajax({
			url : "checkQrcode",
			success : function(result){
				var state = eval(result);
				switch(state){
					case 4:
						$("#qrstate").text("微信扫码成功");
						break;
					case 1:
						$("#qrstate").text("登陆成功");
						clearInterval(timer); //关闭定时器
						break;
					case 0:
						$("#qrstate").text("获取token失败");
						break;
					default:
						$("#qrstate").text("请扫描二维码");
				}
			},
		});
	}
	/*判断公众号是否在登陆状态*/
	function checkLogin(){
		var state = false;
		$.ajax({
			url : "checkLogin",
			async : false, //如果使用异步方式会直接返回state默认值
			success : function(result){
				state = eval(result);
			},
		});
		return state;
	}
	/*获取公众号登陆二维码*/
	function getQrcode(){
		$.ajax({
			url : "getQrcode",
			async : false, 
			success : function(result){
				$("#login").attr(result);
				$("#login").text(result);
				console.log("获取公众号二维码结果："+result);
			},
		});
	}
	/*开始登陆*/
	function login(){
		getQrcode(); // 获取二维码
		$("#qrcode").attr("src","static/images/qrcode.jpg?"+Math.random());
		timer = self.setInterval(checkQrcode,3000); //定时轮询登陆状态
	}
	$(function(){
		var loginState = checkLogin();
		if(loginState==false){
			login();
		}else{
			$("#qrstate").text("已经登录，无需扫描二维码！");
		}
	})
	
</script>
</html>